package com.github.linkoog.model_designer.ide.type;

import com.github.linkoog.model_designer.Icons;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.fileTypes.ex.FileTypeIdentifiableByVirtualFile;
import com.intellij.openapi.util.RecursionGuard;
import com.intellij.openapi.util.RecursionManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.newvfs.impl.StubVirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.yaml.YAMLLanguage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.util.Arrays;
import java.util.List;

public class ModelYamlFileType extends LanguageFileType implements FileTypeIdentifiableByVirtualFile {
    private static final Logger logger = LoggerFactory.getLogger(ModelYamlFileType.class);
    public static final ModelYamlFileType INSTANCE = new ModelYamlFileType();
    public static final String DEFAULT_EXTENSION = "model";
    public static final List<String> EXTENSIONS = Arrays.asList("model","dbm-model");

    private static final String GUARD_ID = "DBModelYamlFileType";
    private final RecursionGuard recursionGuard = RecursionManager.createGuard(GUARD_ID);

    private ModelYamlFileType() {
        super(YAMLLanguage.INSTANCE);
    }

    @Override
    public boolean isMyFileType(@NotNull VirtualFile file) {
        if (file instanceof StubVirtualFile) {
            return false;
        }

        return (Boolean) recursionGuard.doPreventingRecursion(GUARD_ID, true, () -> {
            if (file.isValid()) {
                final String extension = file.getExtension();
                if (EXTENSIONS.contains(extension)) {
                    return true;
                }
            }
            return false;
        });
    }

    @Override
    public String getName() {
        return "model";
    }

    @Override
    public String getDisplayName() {
        return "model";
    }

    @Override
    public String getDescription() {
        return "model";
    }

    @Override
    public String getDefaultExtension() {
        return DEFAULT_EXTENSION;
    }

    @Override
    public Icon getIcon() {
        return Icons.MODEL;
    }

}
